/**
 * 系统角色
 */
import {Body, Controller, Get, Inject, Post, UsePipes} from "@nestjs/common";
import {ApiOperation, ApiTags} from "@nestjs/swagger";
import {SysRoleService} from "../service/SysRoleService";
import {Result} from "../common/Result";
import {NeedRoles} from "../auth/guard/GlobalGuardProvider";
import {RoleEnum} from "../common/enum/RoleEnum";
import {PageResult} from "../common/page/PageResult";
import {PageQuery} from "../common/page/PageQuery";
import {SysRole} from "../entity/SysRole";
import {SysRoleDto} from "../dto/SysRoleDto";
import {MyDtoSerializePipe} from "../common/pipe/MyDtoSerializePipe";
import {MyValidationPipe} from "../common/pipe/MyValidationPipe";
import {Validate_Mods} from "../common/enum/ValidateModsEnum";

@ApiTags("角色模块")
@Controller("/sysRole")
export class SysRoleController {

    @Inject()
    private sysRoleService: SysRoleService;

    @ApiOperation({summary: "测试权限"})
    @Get("/test/role")
    @NeedRoles(RoleEnum.STORE_MANAGER)
    async test() {
        return Result.success();
    }

    /**
     * 角色列表
     */
    @ApiOperation({summary: "角色列表"})
    @UsePipes(new MyDtoSerializePipe(SysRoleDto))
    @Post("/roleList")
    async roleList(@Body() query: PageQuery<SysRoleDto>) {
        let data = await this.sysRoleService.roleList(query);
        return Result.success("角色列表", PageResult.of(data))
    }

    /**
     * 角色种类查询
     */
    @ApiOperation({summary: "角色种类查询"})
    @Get("/types")
    async types() {
        let data = await this.sysRoleService.types();
        return Result.success("角色列表", data)
    }

    /**
     * 编辑或添加角色
     * @param dto
     */
    @ApiOperation({summary: "编辑\\添加角色"})
    @Post("/upsert")
    async upsertRole(@Body() dto: SysRoleDto) {
        return await this.sysRoleService.upsertRole(dto);
    }

    /**
     * 删除角色
     * @param dto
     */
    @ApiOperation({summary: "删除角色"})
    @UsePipes(new MyValidationPipe([Validate_Mods.delete]))
    @Post("/delete")
    async deleteRole(@Body() dto: SysRoleDto) {
        return await this.sysRoleService.deleteRole(dto);
    }
}
